lib.use(['element', 'table', 'form','tablePlug'], function () {
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var userTable;

    var navHide=common.util.getParam('navHide')=="true";
    var tenantId=common.util.getParam('tenantId')||common.util.getUserTenantId();
    //初始表格基础参数
    table.set(common.layui.table.config());
    // 渲染表格
    userTable = table.render({
          elem: '#userTable'
        , url: common.util.createUrl('/sys/user/list?tenantId='+tenantId)
        , cols: [[
              {type:'checkbox'}
            , {type: 'numbers', title: '编号'}
            , {field: 'id',  hide: true}
            , {field: 'userName', title: '用户名称', minwidth: 250}
            , {field: 'userEmail', title: '邮箱', minwidth: 150}
            , {field: 'userMobile', title: '手机', minwidth: 150}
            , {field: 'userStatus', title: '是否有效', width: 80, templet: function (row) {return common.dist.getValue("comm.ValidEnum",row.userStatus); }}
            , {field: 'updateUserName', title: '更新人', width: 160}
            , {field: 'updateTime', title: '更新时间', width: 160}
            , {
                field: 'id', fixed: 'right', title: '操作', width: 140, templet: function (row) {
                    return '<a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">修改</a>' +
                        '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                }
            }
        ]]
    });

    $(document).on('click','#add',function(){
        var config = {
            title:"添加用户",
            content: 'sys/user/userForm.html?navHide='+navHide+'&tenantId='+tenantId //iframe的url，no代表不显示滚动条
        }
        common.util.openWindow(busCommon.baseWindowConfig(config))
    });

    //监听单元格事件
    table.on('tool(userTableFilter)', function(obj) {
        if (obj.event === 'edit') {
            var config = {
                title:"添加用户",
                content: 'sys/user/userForm.html?id='+obj.data.id+'&navHide='+navHide+'&tenantId='+tenantId //iframe的url，no代表不显示滚动条
            }
            common.util.openWindow(busCommon.baseWindowConfig(config))
        }
        if (obj.event === 'del') {
            del(obj);
        }
    });

    form.on('submit(searchFilter)', function(data){
        var params = data.field;
        if(!params){
            params=common.from.serializeObject('#searchFrm');
        }
        searchTable(params)
        return false;
    });


    /**
     * 查询表格
     * @param params
     */
    function searchTable(params) {
       userTable.reload({where: params })
    }

    /**
     * 删除
     * @param obj
     */
    function del(obj) {
        var postData = [];
        if(obj.data.constructor === Array){
            postData=obj.data.map(function (item) {
                return item['id'];
            });
        }else{
            postData=[obj.data.id]
        }
        if(postData.length==0)return;
        layer.confirm('真的删除吗', function(index){
            console.log('Role Del param is', postData);
            common.ajax.post('/sys/user/delete', postData, function (resp) {
                searchTable();
                layer.close(index);
                common.util.alert('审核成功')
            })
        });
    }
    (function () {
        if(navHide)$(".x-nav").hide();
    })();
});
